package com.sdyeda.readiot.iotinfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadingInfo {

	private  Date begin;
	private Date end;
	private boolean reading;

	private List<IotData> list;
	
	public static List<String> getComs(List<IotData> list){
		List<String> coms = new ArrayList<>();
		list.forEach(item ->{
			if(!coms.contains(item.getCom())) {
				coms.add(item.getCom());
			}
		});
		return coms;
	}

	public int getSumReadedIots() {
		int result = 0;
		for(int i=0;i<list.size();i++) {
			if (list.get(i).getReadtimes()>0) {
				result ++;
			}
		}
		return result;
	}
	
	public int getSumIots() {
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	public int getSumSuccIots() {
		int result = 0;
		if (list != null) {
			for(int i=0;i<list.size();i++) {
				if((list.get(i)).isSuccess()) {
					result++;
				}
			}
		}
		return result;
	}

	public Map<String, Integer> getComSumIots() {
		Map<String, Integer> sumcomiots = new HashMap<String, Integer>();

		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				IotData item = list.get(i);
				sumcomiots.put(item.getCom(),
						sumcomiots.get(item.getCom()) == null ? 1 : sumcomiots.get(item.getCom()) + 1);
			}
		}
		return sumcomiots;
	}

	public Map<String, Integer> getComSumSuccIots() {
		Map<String, Integer> sumcomsucc = new HashMap<String, Integer>();

		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				IotData item = list.get(i);
				if (item.isSuccess()) {
					sumcomsucc.put(item.getCom(),
							sumcomsucc.get(item.getCom()) == null ? 1 : sumcomsucc.get(item.getCom()) + 1);
				} else {
					sumcomsucc.put(item.getCom(),
							sumcomsucc.get(item.getCom()) == null ? 0 : sumcomsucc.get(item.getCom()));
				}
			}
		}
		return sumcomsucc;

	}

	public Map<String, Integer> getTypeSumIots() {
		Map<String, Integer> sumtrpeiots = new HashMap<String, Integer>();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				IotData item = list.get(i);
				sumtrpeiots.put(item.getType(),
						sumtrpeiots.get(item.getType()) == null ? 1 : sumtrpeiots.get(item.getType()) + 1);
			}
		}
		return sumtrpeiots;
	}

	public Map<String, Integer> getTypeSumSuccIots() {
		Map<String, Integer> sumtypesucc = new HashMap<String, Integer>();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				IotData item = list.get(i);
				// sumtrpeiots.put(item.getType(), sumtrpeiots.get(item.getType())==null?
				// 1:sumtrpeiots.get(item.getType())+1);
				if (item.isSuccess()) {
					sumtypesucc.put(item.getType(),
							sumtypesucc.get(item.getType()) == null ? 1 : sumtypesucc.get(item.getType()) + 1);
				} else {
					sumtypesucc.put(item.getType(),
							sumtypesucc.get(item.getType()) == null ? 0 : sumtypesucc.get(item.getType()));
				}
			}
		}
		return sumtypesucc;
	}

	public ReadingInfo() {

		setBegin(new Date());
		setEnd(new Date());
		list = null;
	}

	public void Init(List<IotInfo> iotinfos) {
		setBegin(new Date());
		setEnd(new Date());

		setList(new ArrayList<>());

		if (iotinfos != null) {
			iotinfos.forEach(item -> {
				list.add(new IotData(item));
			});
		}
	}

	public void setIotData(IotData iotdata) {
		for (int i = 0; i < list.size(); i++) {
			//System.out.println(list.get(i).getIotid());
			//System.out.println(iotdata.getIotid());
			if (list.get(i).getIotid().equals(iotdata.getIotid())) {
				
				//System.out.println();
				list.set(i, iotdata);
			}
		}
		//System.out.print(index);
		setEnd(new Date());
	}

	public Date getBegin() {
		return begin;
	}

	public void setBegin(Date begin) {
		this.begin = begin;
		this.end = begin;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public List<IotData> getList() {
		return list;
	}

	public void setList(List<IotData> list) {
		this.list = list;
	}

	public boolean isReading() {
		return reading;
	}

	public void setReading(boolean reading) {
		this.reading = reading;
		if(reading) {
			setBegin(new Date());
		}
		else {
			setEnd(new Date());
		}
	}

}
